<script setup name="myabsent">
import { ref, reactive, onMounted, computed, watch } from 'vue';
import absentHttp from '@/api/absentHttp';
import { ElMessage } from 'element-plus';
import timeFormatter from '@/utils/timeFormatter';
import OAPagination from '@/components/OAPagination.vue';
import OADialog from '@/components/OADialog.vue';
import OAMain from '@/components/OAMain.vue';

let dialogFormVisible = ref(false)
let formLabelWidth = "100px"
let absentForm = reactive({
    title: '',
    absent_type_id: null,
    date_range: [],
    request_content: ''
})
//个人考勤信息
let absents = ref([])

let absent_types = ref([])
let absentFormRef = ref()
let rules = reactive({
    title: [
        { required: true, message: '请输入标题！', trigger: 'blur' }
    ],
    absent_type_id: [
        { required: true, message: '请选择请假类型！', trigger: 'change' }
    ],
    date_range: [
        { required: true, message: '请选择请假时间！', trigger: 'blur' }
    ],
    request_content: [
        { required: true, message: '请输入请假理由！', trigger: 'blur' }
    ]
})
let responser = reactive({
    // email: '',
    // realname: ''
})
let responser_str = computed(() => {
    console.log(responser.email)
    if (responser.email) {
        console.log(responser)
        return '[' + responser.email + ']' + responser.realname
    } else {
        return "无"
    }
})

let pagination = reactive({
    total: 0,
    page: 1,
})

const onShowDialog = () => {
    absentForm.title = ''
    absentForm.absent_type_id = null
    absentForm.date_range = []
    absentForm.request_content = ''
    dialogFormVisible.value = true;
}

const onSubmitAbsent = () => {
    console.log(absentFormRef.value)
    absentFormRef.value.validate(async (valid, fields) => {
        if (valid) {
            let data = {
                title: absentForm.title,
                absent_type_id: absentForm.absent_type_id,
                start_date: absentForm.date_range[0],
                end_date: absentForm.date_range[1],
                request_content: absentForm.request_content
            }
            console.log(data)
            try {
                let absent = await absentHttp.applyAbsent(data)
                dialogFormVisible.value = false
                ElMessage.success('请假成功');
                absents.value.unshift(absent)
            } catch (err) {
                ElMessage.error(err)
            }
        }
    })
}

onMounted(async () => {
    try {
        //1.获取请假类型
        absent_types.value = await absentHttp.getAbsentTypes()
        //2.获取审批者
        let responser_data = await absentHttp.getResponser()
        Object.assign(responser, responser_data)
        // responser = responser_data
        console.log(responser)
        //3.获取个人考勤信息
        requestAbsents(1)
    } catch (err) {
        ElMessage.error(err)
    }
})


//监听当前页码，如果pagination.page发生改变，从后端传入对应的页面数据
watch(() => pagination.page, (value) => {
    requestAbsents(value)
})

const requestAbsents = async (page) => {
    try {
        let absents_data = await absentHttp.getMyAbsents(page)
        let total = absents_data.count;
        pagination.total = total
        let results = absents_data.results;
        absents.value = results;
        console.log(absents)
    } catch (err) {
        ElMessage.error(err)
    }

}

</script>


<template>
    <OAMain title="个人考勤">
        <el-card style="text-align: right;">
            <el-button type="primary" @click="onShowDialog"><el-icon>
                    <Plus />
                </el-icon>发起考勤</el-button>
        </el-card>
        <el-card>
            <el-table :data="absents" style="width: 100%">
                <el-table-column prop="title" label="标题" />
                <el-table-column prop="absent_type.name" label="类型" />
                <el-table-column prop="request_content" label="原因" />
                <el-table-column label="发起时间">
                    <template #default="scope">
                        {{ timeFormatter.stringFromDateTime(scope.row.create_date) }}
                    </template>
                </el-table-column>
                <el-table-column label="开始日期">
                    <template #default="scope">
                        {{ timeFormatter.stringFromDate(scope.row.start_date) }}
                    </template>
                </el-table-column>
                <el-table-column label="结束日期">
                    <template #default="scope">
                        {{ timeFormatter.stringFromDate(scope.row.end_date) }}
                    </template>
                </el-table-column>
                <el-table-column label="审核领导">
                    {{ responser_str }}
                </el-table-column>
                <el-table-column prop="response_content" label="反馈意见" />
                <el-table-column label="审核状态">
                    <template #default="scope">
                        <el-tag type="info" v-if="scope.row.status == 1">审核中</el-tag>
                        <el-tag type="success" v-else-if="scope.row.status == 2">已通过</el-tag>
                        <el-tag type="danger" v-else>已拒绝</el-tag>
                    </template>
                </el-table-column>
            </el-table>
            <template #footer>
                <!-- <el-pagination background layout="prev, pager, next" :total="pagination.total" :page-size="10"
                    v-model:current-page="pagination.page" /> -->
                <OAPagination v-model="pagination.page" :total="pagination.total"></OAPagination>
            </template>
        </el-card>
    </OAMain>

    <OADialog title="发起请假" v-model="dialogFormVisible" @submit="onSubmitAbsent">
        <el-form :model="absentForm" :rules="rules" ref="absentFormRef">
            <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
                <el-input placeholder="请输入标题" v-model="absentForm.title" autocomplete="on" />
            </el-form-item>
            <el-form-item label="请假类型" :label-width="formLabelWidth" prop="absent_type_id">
                <el-select v-model="absentForm.absent_type_id" placeholder="请选择请假类型">
                    <el-option v-for="item in absent_types" :label="item.name" :value="item.id" :key="item.name" />
                </el-select>
            </el-form-item>
            <el-form-item label="请假时间" :label-width="formLabelWidth" prop="date_range">
                <el-date-picker v-model="absentForm.date_range" type="daterange" range-separator="到"
                    start-placeholder="起始日期" end-placeholder="结束日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="审批领导" :label-width="formLabelWidth">
                <el-input :value="responser_str" readonly disabled autocomplete="off" />
            </el-form-item>
            <el-form-item label="请假理由" :label-width="formLabelWidth" prop="request_content">
                <el-input :rows="4" type="textarea" v-model="absentForm.request_content" placeholder="请输入请假理由"
                    autocomplete="off" />
            </el-form-item>
        </el-form>
    </OADialog>

</template>


<style scoped></style>